package com.prs.crm.action.purchase.flow;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.purchase.Order;
import com.prs.crm.domain.purchase.OrderType;
import com.prs.crm.domain.purchase.SupplierInquiry;
import com.prs.crm.service.purchase.OrderService;
import com.prs.crm.service.purchase.OrderTypeService;
import com.prs.crm.service.purchase.SupplierInquiryService;


public class OrderFlowAction extends FlowAction<Order>{

	private static final long serialVersionUID = 3690311264524103659L;
	
	private Order order;
	private Collection<OrderType> orderTypes;
	private Collection<SupplierInquiry> supplierInquirys;
	private OrderService orderService;
	private SupplierInquiryService supplierInquiryService;
	private OrderTypeService orderTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private File upload;
	private String uploadFileName;
	
	@Action("listOrder")
	public String listOrder(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(SupplierInquiry.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSupplierInquiryService().pageQueryValid(criteria, pagination));
		return SUCCESS;
	}
	
	@Action(value="editOrderFlow",results={@Result(name="error",type="chain",location="listOrder")})
	public String editOrderFlow(){
		orderTypes = this.getOrderTypeService().find(this.getPaginationHelper().createQuery(OrderType.class));
		if(ids != null && ids.length > 0)
			supplierInquirys = this.getSupplierInquiryService().findByIds(ids);
		else{
			this.setPrompt("请选择采购单");
			return ERROR;
		}
		return SUCCESS;
	}
	@Clew(results={"success","none"})
	@Action(value="saveOrderFlow",results={@Result(name="success",type="redirect",location="listOrder"),
			@Result(name="error",type="chain",location="editOrderFlow")})
	public String saveOrderFlow(){
		if (supplierInquirys != null && supplierInquirys.size() > 0) {
			for(SupplierInquiry supplierInquiry : supplierInquirys){
				order.addSupplierInquiry(supplierInquiry);
			}
		}else{
			this.setPrompt("请选择采购单");
			return ERROR;
		}
		if(order.getId() == null){
			this.getOrderService().saveOrder(order, upload, uploadFileName);
			this.setPrompt("生成订单成功");
		}else{
			this.getOrderService().saveOrder(order, upload, uploadFileName);
			this.setPrompt("修改订单成功");
		}
		return SUCCESS;
	}
	
	
	
	

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public SupplierInquiryService getSupplierInquiryService() {
		return supplierInquiryService;
	}

	public void setSupplierInquiryService(
			SupplierInquiryService supplierInquiryService) {
		this.supplierInquiryService = supplierInquiryService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public OrderTypeService getOrderTypeService() {
		return orderTypeService;
	}

	public void setOrderTypeService(OrderTypeService orderTypeService) {
		this.orderTypeService = orderTypeService;
	}

	public Collection<OrderType> getOrderTypes() {
		return orderTypes;
	}

	public void setOrderTypes(Collection<OrderType> orderTypes) {
		this.orderTypes = orderTypes;
	}

	public Collection<SupplierInquiry> getSupplierInquirys() {
		return supplierInquirys;
	}

	public void setSupplierInquirys(Collection<SupplierInquiry> supplierInquirys) {
		this.supplierInquirys = supplierInquirys;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	

}
